package com.zh.work.controller.platform.virtual;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.entity.JSONResult;
import com.zh.work.dao.platform.virtual.SystemHostMapper;
import com.zh.work.entity.virtual.SystemHostEntity;
import com.zh.work.service.platform.virtual.SystemHostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 白名单主机管理控制器
 * @Author: Li.HuiNeng
 * @Create in: 2020-02-04 9:30
 * @Version: 1.0.1
 */
@Api(value = "白名单主机管理控制器", produces = "application/json", tags = {"白名单主机管理控制器"})
@RestController
@RequestMapping("/host")
public class SystemHostController {
    
    @Autowired
    private SystemHostMapper mapper;
    
    
    @Autowired
    private SystemHostService service;


    @ApiOperation(value = "新增一条记录", tags = {"putAddSystemHost"}, notes = "新增一条记录")
    @PutMapping("/putAddSystemHost")
    @ResponseBody
    public JSONResult<Object> putAddSystemHost(@ApiParam(value = "费率实体类", name = "systemHostEntity")
                                                    @RequestBody SystemHostEntity systemHostEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (systemHostEntity.insert()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("添加完成!");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("请添加正确参数!");
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "通过id修改信息", tags = {"putModSystemHostById"}, notes = "通过id修改信息")
    @PutMapping("/putModSystemHostById")
    @ResponseBody
    public JSONResult<Object> putModSystemHostById(@ApiParam(value = "费率实体类", name = "systemHostEntity")
                                                        @RequestBody SystemHostEntity systemHostEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (systemHostEntity.updateById()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("修改完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("请添加正确参数信息!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据主键删除记录", tags = {"deleteSystemHostById"}, notes = "根据主键删除记录")
    @DeleteMapping("/deleteSystemHostById")
    public JSONResult<Object> deleteSystemHostById(@ApiParam(value = "费率实体类", name = "SystemHostEntity") SystemHostEntity systemHostEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (systemHostEntity.deleteById()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据主键列表批量删除记录", tags = {"deleteSystemHostBatchByIds"}, notes = "该传参时 请选择raw(application/json)格式传入")
    @DeleteMapping("/deleteSystemHostBatchByIds")
    @ResponseBody
    public JSONResult<Object> deleteSystemHostBatchByIds(@ApiParam(value = "主键集合", name = "ids")@RequestParam("ids") @RequestBody List<Integer> ids) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int c = this.mapper.deleteBatchIds(ids);
            if (c > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }



    @ApiOperation(value = "分页查询参数数据集", tags = {"getPagesSystemHosts"}, notes = "分页查询参数数据集")
    @GetMapping("/getPagesSystemHosts")
    public JSONResult<List<SystemHostEntity>> getPagesSystemHosts(
            @ApiParam(value = "起始页", name = "pageNo")Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageSize")Integer pageSize,
            @ApiParam(value = "白名单主机实体类", name = "SystemHostEntity") SystemHostEntity systemHostEntity) {
        JSONResult<List<SystemHostEntity>> jsonResult = new JSONResult<>();
        try {
            IPage<SystemHostEntity> result = this.service.selectSystemHostPages(new Page<>(pageNo, pageSize), systemHostEntity);
            jsonResult.setTotal(result.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }
}
